﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InformationExtraction
{
    class Program
    {
        static void Analyze(string path, bool printProcess, out Extractor extractor)
        {
            extractor = new Extractor(new ExtractorProcess[] 
            { 
                new BirthdayProcess(), 
                new NameProcess(),
                new BornInProcess(),
                new CountryProcess(),
                new OccupationProcess(),
                new GenderProcess(),
                new IsAliveProcess()
            });

            //analyze
            Console.WriteLine("Reading \"" + path + "\"");
            Console.WriteLine();
            long milli = extractor.Analyze(path, printProcess, 50600);
            Console.WriteLine("Done");

            //print: general info
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Number of Pages Read: " + extractor.NumOfPages());
            Console.WriteLine("Total Analysis Time (milliseconds): " + milli);
            Console.WriteLine();
            Console.WriteLine();
        }

        // print 3 random pages
        static void RandomPages(Extractor extractor)
        {
            Console.WriteLine();
            Console.WriteLine();
            Random rand = new Random((int)DateTime.Now.Ticks);
            int i = rand.Next(extractor.NumOfPages() - 3);
            extractor.PrintPages(i, i + 3);
        }

        // print 10 random members of the ontology
        static void RandomOntology(Extractor extractor)
        {
            Console.WriteLine();
            Console.WriteLine();
            Random rand = new Random((int)DateTime.Now.Ticks);
            int i = rand.Next(extractor.OntologySize() - 30);
            extractor.PrintOntology(i, i + 30);
        }

        // search page
        static void PrinthPage(bool printProcess, Extractor extractor)
        {
            Console.WriteLine();
            Console.Write("Enter page name/number: ");
            string query = Console.ReadLine();
            Console.WriteLine("Show POS ? (y/n)");
            bool showRaw = Console.ReadKey().KeyChar == 'y';
            Console.WriteLine();
            Console.WriteLine();
            extractor.SearchPage(query, showRaw);
        }

        // query the ontology & print person details
        static void PrintPerson(bool printProcess, Extractor extractor)
        {
            Console.WriteLine();
            Console.Write("Enter a person name: ");
            string personName = Console.ReadLine();
            Console.WriteLine();
            Console.WriteLine();
            Person[] res = extractor.SearchPersons(personName, printProcess);
            extractor.PrintPersons(res);
        }

        // print the on screen menu
        static void PrintMenu()
        {
            Console.WriteLine();
            Console.WriteLine("Enter your choice: ");
            Console.WriteLine(" 1. Print 3 random pages");
            Console.WriteLine(" 2. Print 10 random persons");
            Console.WriteLine(" 3. Print sepcific page");
            Console.WriteLine(" 4. Query for a specific person");
            Console.WriteLine();
            Console.WriteLine("or Enter 'q' to Quit");
        }        
        
        static void Main(string[] args)
        {
            Extractor extractor;
            string path = "full_pos.txt";
            bool printProcess = true;

            Analyze(path, printProcess, out extractor);

            extractor.PrintPersons(extractor.SearchPersons(new Person()
            {
                Birthday = new DateTime(1912, 1, 1),
                Country = "English"
            }));


            PrintMenu();
            ConsoleKeyInfo key;
            while ((key = Console.ReadKey()).KeyChar != 'q')
            {
                switch (key.KeyChar)
                {
                    case '1':
                        RandomPages(extractor);
                        break;
                    case '2':
                        RandomOntology(extractor);
                        break;
                    case '3':
                        PrinthPage(printProcess, extractor);
                        break;
                    case '4':
                        PrintPerson(printProcess, extractor);
                        break;
                    case 'q':
                        Console.WriteLine();
                        Console.WriteLine("Bye!");
                        break;
                }
                PrintMenu();
            } 
        }
    }
}
